#include <cocos2d.h>
#include "Movedown.h"
#include "HelloWorldScene.h"
using namespace cocos2d;

MoveDown::MoveDown(){

	setName("MoveDown");
	
	add_count = 0;

	mXAdd = 0;

	fRandomArray(arr_obstructions);
}

MoveDown::MoveDown(Monkey* monkey){

	setName("MoveDown");

	add_count = 0;

	mXAdd = 0;

	mMonkey = monkey;

	fRandomArray(arr_obstructions);
}

void MoveDown::fActionMoveDown(){

	CCMoveBy* actionMove = CCMoveBy::create(kTreeMoveUpSpeed, ccp(0, -10));

	parent->runAction(CCRepeatForever::create(actionMove));
	
}

void MoveDown::fActionAddBodyTree(){
	

	if( parent->getPositionY() + parent->getContentSize().height/2 < parent->mSize.height)
	{
		//CCDirector::sharedDirector()->pause();
		CCSprite * body ;
		//if(add_count == kMaxLenght + 1)
		//{
		//	parent->removeComponent("MoveDown");
		//	((HelloWorld*)parent->getParent())->fAddTree();
		//	//parent->getParent()->removeChild(parent);
		//	CCLOG("%d",add_count);
		//}

		CCRect nRectBodyMonkey = mMonkey->fGetArmature()->getBone("Layer6")->getDisplayRenderNode()->boundingBox();

		if(parent->getPositionY() + parent->getContentSize().height/2 < nRectBodyMonkey.getMaxY() + mMonkey->getPositionY()
			&& parent->getPositionX() + parent->getContentSize().width/2 > nRectBodyMonkey.getMinX() + mMonkey->getPositionX()
			&& parent->getPositionX() - parent->getContentSize().width/2 < nRectBodyMonkey.getMaxX() + mMonkey->getPositionX()
			)

		{
			CCLOG("Game over");
			mMonkey->fSetStatus(isDie);
		}
		if(add_count == kMaxLenght)
		{
			body = CCSprite::create("bambooheader.png");


			parent->addChild(body, kZorderTree);
			body->setPosition(ccp( 0,parent->getContentSize().height/2+body->getContentSize().height/2));
			//parent->setContentSize(CCSize(parent->getContentSize().width , parent->getContentSize().height +  body->getContentSize().height*2 ));
		
			body->addComponent(new RemoveWhenDown(1));

		}
		else if(add_count <= kMaxLenght - 1)
		{
			if(add_count == kMaxLenght-1)
			{
				CCPoint p;
				CCSize nSize = CCDirector::sharedDirector()->getWinSize();
				p.setPoint(rand()%2,0);

				//parent->removeComponent("MoveDown");
				//parent->removeComponent("Move");
			

				((HelloWorld*)parent->getParent())->fAddTree(p);
				//parent->getParent()->removeChild(parent);
			}

			body = CCSprite::create("bamboobody.png");

			parent->addChild(body, kZorderTree);
			body->setPosition(ccp(0,parent->getContentSize().height/2 + body->getContentSize().height/2));


			/*switch(arr_obstructions[add_count])
			{
			case 0:
				fAddFruit(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/4));
				fAddBough(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			case 1:
				fAddEnemy(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/4));
				fAddFruit(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			case 2:
				fAddEnemy(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			case 3:
				fAddEnemy(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			case 4:
				fAddFruit(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			default:
				CCLOG("DEFAULT CMNR");
				fAddBough(ccp(parent->getContentSize().width/2 - 1, parent->getContentSize().height + body->getContentSize().height/2));
				break;
			}*/
			
			//fAddEnemy(ccp( 0,parent->getContentSize().height/2));
			//fAddFruit(ccp(parent->getContentSize().width/2, parent->getContentSize().height/2 + body->getContentSize().height/4));
			parent->setContentSize(CCSize(parent->getContentSize().width , parent->getContentSize().height +  body->getContentSize().height*2 ));
		
			body->addComponent(new RemoveWhenDown());
			
			
		}

		add_count++;
	}
}

void MoveDown::fAddFruit(CCPoint p)
{
	srand((time(0)*100));
	int nIsLeft = rand()%2;
	int nType = rand()%3;
	CCPoint nPoint;
	if(nIsLeft == 1)
	{
		nPoint.setPoint(p.x - parent->getContentSize().width/2, p.y);
	}
	else
	{
		nPoint.setPoint(p.x + parent->getContentSize().width/2, p.y);
	}
	Fruit* nFruit = new Fruit(nType, nPoint, nIsLeft);
	nFruit->addComponent(new MonkeyCollision(mMonkey, kFruit));
	parent->addChild(nFruit,4);
}

void MoveDown::fAddBough(CCPoint p)
{
	srand((time(0)*100));
	int nIsLeft = rand()%2;
	int nType = rand()%2;
	CCPoint nPoint;
	if(nIsLeft == 1)
	{
		nPoint.setPoint(p.x - parent->getContentSize().width/2, p.y);
	}
	else
	{
		nPoint.setPoint(p.x + parent->getContentSize().width/2, p.y);
	}
	Bough* nBough = new Bough(BTYPE1, nPoint, nIsLeft);
	parent->addChild(nBough,5);
	nBough->addComponent(new MonkeyCollision(mMonkey, kBough));
}


void MoveDown::fAddEnemy(CCPoint p)
{
	srand((time(0)*100));
	int nIsLeft = rand()%2;
	int nType = rand()%3;
	CCPoint nPoint;
	if(nIsLeft == 1)
	{
		nPoint.setPoint(p.x - parent->getContentSize().width/2, p.y);
	}
	else
	{
		nPoint.setPoint(p.x + parent->getContentSize().width/2, p.y);
	}
	Enemy* nEnemy = new Enemy(BTYPE1, nPoint, nIsLeft);
	parent->addChild(nEnemy,6);
	nEnemy->addComponent(new MonkeyCollision(mMonkey, kEnemy));
}

void MoveDown::update(float dt){

	parent = (Tree*) getOwner();

	if(parent == NULL){
		return;
	}

	if(parent->initTree == 1)
	{
		parent->initTree = 0;

		parent->setContentSize(parent->mTreeSize);
	}

	fActionAddBodyTree();

	if(parent->flagMoving == 1){
		fActionMoveDown();

		parent->flagMoving = 0;
	}
	
}

void MoveDown::fRandomArray(int *arr)
{
	for(int i = 0; i < kMaxLenght - 1; i++)
	{
		int value = CCRANDOM_0_1() * kMaxRand;

		if(i != 0 && i != 1)
		{
			int index0 = arr[i-2];
			int index1 = arr[i-1];

			if(index0 == index1)
			{
				value = index1 + 1;
				if(value == kMaxRand + 1)
				{
					value -= 2;
				}
			}
		}
		
		arr[i] = value;
	}
	arr[kMaxLenght - 1] = kMaxRand;
}

RemoveWhenDown::RemoveWhenDown()
{
	setName("RemoveWhenDown");
}
RemoveWhenDown::RemoveWhenDown(int type)
{
	setName("RemoveWhenDown");

	isFinish = 1;
}

void RemoveWhenDown::update(float dt)
{
	parent = (CCSprite*)getOwner();

	if(parent == NULL)
		return;

	if(parent->getParent()->convertToWorldSpace(parent->getPosition()).y + parent->getContentSize().height/2 < 0){

		if(isFinish == 1)
		{
			isFinish = 0;

			parent->getParent()->getParent()->removeChild(parent->getParent(), true);
		}
		else
		{
			parent->removeAllComponents();
			parent->getParent()->getParent()->removeChild(parent, true);
			parent->getParent()->removeChild(parent, true);
		}
	}
}

